package com.paas.form.core.element.base;

import com.gencode.util.CodeBuildNew;
import com.paas.common.util.BeanUtil;
import com.paas.common.util.StringUtil;
import com.paas.form.core.button.Button;
import com.paas.form.core.element.FieldElement;
import com.paas.form.core.element.constants.TableConstant;
import com.paas.form.core.element.field.ColumnListModel;
import com.paas.form.core.element.field.TableField;
import com.paas.form.core.element.jsContent.ScriptContent;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.common.TemplateParserContext;
import org.springframework.expression.spel.standard.SpelExpressionParser;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Table implements FieldElement {
    List<ColumnListModel> fields;
    List<Button> buttonList;
    Map<String, Object> map = new HashMap<>();

    public Table(Map<String, Object> configMap,List<Button> buttonList,List<ColumnListModel> fields){
        this.buttonList = buttonList;
        this.fields = fields;
        this.map.putAll(configMap);
    }




    private String buildTableHtml() {
        if (fields == null || fields.size() == 0) {
            return "";
        }
        StringBuffer TableHtml =new StringBuffer();
        TableHtml.append(TableConstant.VM_TABLE_HEAD_HTMl);

        for (ColumnListModel tableField:fields) {
            map.put("field",tableField);

            //根据不同字段类型填充，暂时填充默认
            String columnString = CodeBuildNew.getCodeString(map,TableConstant.VM_TABLE_COLUMN_NONE);
            TableHtml.append("\n"+columnString);
        }
        //表格按钮部分
        if(BeanUtil.isNotEmpty(buttonList)&&buttonList.size()>0){
            TableHtml.append(TableConstant.VM_TABLE_COLUMN_BUTTON_START);
            for (Button button:buttonList) {
                map.put("button",button);
                String buttonString = CodeBuildNew.getCodeString(map,TableConstant.VM_TABLE_COLUMN_BUTTON);
                TableHtml.append("\n"+buttonString);

            }
            TableHtml.append(TableConstant.VM_TABLE_COLUMN_BUTTON_END);

        }
        TableHtml.append(TableConstant.VM_TABLE_END_HTMl);

        //加分页部分
        TableHtml.append(TableConstant.VM_TABLE_PAGINATION);

        return TableHtml.toString();
    }

    private ScriptContent buildTableJs(){

        ScriptContent scriptContent = new ScriptContent();


        scriptContent.setData(TableConstant.VM_TABLE_JS_DATA);
        String newMethodsStr = CodeBuildNew.getCodeString(map,TableConstant.VM_TABLE_JS_METHODS_GETDATALIST);
        scriptContent.setMethods(newMethodsStr);

        //有按钮则添加按钮的相关js
        ExpressionParser parser = new SpelExpressionParser();
        TemplateParserContext parserContext = new TemplateParserContext("${", "}");
        Map<String, Object> params = new HashMap<>();
        params.put("className", StringUtil.isNotEmpty(this.map.get("className").toString()) ?  this.map.get("className").toString()  : " ");
        String newstr = parser.parseExpression(TableConstant.VM_TABLE_JS_METHODS_BUTTON_FUNC, parserContext).getValue(params, String.class);

//            ScriptContent buttonjs = new ScriptContent();
//            buttonjs.setMethods(CodeBuildNew.getCodeString(map,TableConstant.VM_TABLE_JS_METHODS_BUTTON_FUNC));
//            scriptContent = Utilmethod.scriptContentAdd(scriptContent,buttonjs);
        scriptContent.setMethods(scriptContent.getMethods() + newstr);
        if(BeanUtil.isNotEmpty(buttonList)&&buttonList.size()>0){

        }
        scriptContent.setImports(CodeBuildNew.getCodeString(map,TableConstant.VM_TABLE_JS_IMPORT));







        return scriptContent;
    }


    @Override
    public String makeVue() {
        return buildTableHtml();
    }

    @Override
    public ScriptContent makeJs() {
        return buildTableJs();
    }
}
